﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;

namespace IRUM.TR
{
    /// <summary>
    /// 선물옵션 과거데이터시간대별조회
    /// </summary>
    public class ChartData
    {
        public string shcode = string.Empty;
        public string gubun = string.Empty;
        public string date = string.Empty;  //"yyyy/mm/dd"
        public string time = string.Empty;  //"hh:mm:ss
        public float open = 0;
        public float high = 0;
        public float low = 0;
        public float close = 0;
        public long jdiff_vol = 0;
        public long openyak = 0;
    }

    public class ChartDataArgs : EventArgs
    {
        public List<ChartData> RetData = null;

        public ChartDataArgs(List<ChartData> data)
        {
            RetData = data;
        }
    }

    public delegate void ChartDataHandler(object s, ChartDataArgs e);

    /// <summary>
    /// 선물옵션 과거데이터시간대별 조회
    /// </summary>
    public class t8427 : CBaseQuery
    {
        public event ChartDataHandler OnReceived;
        private List<ChartData> m_list = new List<ChartData>();

        private string fo_gbn = string.Empty;
        private string yyyy = string.Empty;
        private string mm = string.Empty;
        private string cp_gbn = string.Empty;
        private float actprice = 0;
        private string focode = string.Empty;
        private string dt_gbn = string.Empty;
        private string min_term = string.Empty;
        private string date = string.Empty;
        private string time = string.Empty;

        /// <summary>
        /// 선물옵션 과거데이터시간대별조회
        /// </summary>
        /// <param name="fo_gbn">선물옵션구분, 1, F:선물, O:옵션</param>
        /// <param name="yyyy">조회년도, 4</param>
        /// <param name="mm">조회월, 2</param>
        /// <param name="cp_gbn">옵션콜풋구분, 1, 2:콜, 3:풋</param>
        /// <param name="actprice">옵션행사가, 6.2</param>
        /// <param name="focode">선물옵션코드, 8</param>
        /// <param name="dt_gbn">일분구분, 1, D:일, M:분</param>
        /// <param name="min_term">분간격, 2</param>
        public t8427(string fo_gbn, string yyyy, string mm, string cp_gbn, float actprice, string focode, string dt_gbn, string min_term)
        {
            this.fo_gbn = fo_gbn;
            this.yyyy = yyyy;
            this.mm = mm;
            this.cp_gbn = cp_gbn;
            this.actprice = actprice;
            this.focode = focode;
            this.dt_gbn = dt_gbn;
            this.min_term = min_term;

            SetClassData(TR_Type.t8427);
        }

        public void SetOutBlockData(int Index)
        {
            ChartData cd = new ChartData();
            cd.shcode = focode;
            cd.gubun = dt_gbn;
            cd.date = m_Query.GetFieldData(m_OutBlock1Name, "date", Index);
            cd.time = m_Query.GetFieldData(m_OutBlock1Name, "time", Index);
            cd.open = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "open", Index));
            cd.high = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "high", Index));
            cd.low = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "low", Index));
            cd.close = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "close", Index));
            cd.jdiff_vol = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "volume", Index)); //거래량
            cd.openyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "openyak", Index));

            m_list.Add(cd);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlockName);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            //연속조회
            if (m_Query.IsNext == true)
            {
                this.date = m_Query.GetFieldData(m_OutBlockName, "date", 0);
                this.time = m_Query.GetFieldData(m_OutBlockName, "time", 0);
                RequestNext();
            }
            else
            {
                if (OnReceived != null)
                {
                    OnReceived(this, new ChartDataArgs(m_list));
                }
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "fo_gbn", 0, fo_gbn);  //선물옵션구분, 1, F:선물, O:옵션
            m_Query.SetFieldData(m_InBlockName, "yyyy", 0, yyyy);    //조회년도, 4
            m_Query.SetFieldData(m_InBlockName, "mm", 0, mm);      //조회월, 2
            m_Query.SetFieldData(m_InBlockName, "cp_gbn", 0, cp_gbn);  //옵션콜풋구분, 1, 2:콜, 3:풋
            m_Query.SetFieldData(m_InBlockName, "actprice", 0, actprice.ToString("F2"));//옵션행사가, 6.2
            m_Query.SetFieldData(m_InBlockName, "focode", 0, focode);  //선물옵션코드, 8
            m_Query.SetFieldData(m_InBlockName, "dt_gbn", 0, dt_gbn);  //일분구분, 1, D:일, M:분
            m_Query.SetFieldData(m_InBlockName, "min_term", 0, min_term);//분간격, 2
            m_Query.SetFieldData(m_InBlockName, "date", 0, date);    //날짜, 8, 처음 조회시 Space, 다음 조회시 OutBlock의 date 값 입력
            m_Query.SetFieldData(m_InBlockName, "time", 0, time);    //시간, 6, 처음 조회시 Space, 다음 조회시 OutBlock의 time 값 입력
        }
    }
}
